<?php
namespace app\index\model;

use think\Model;

class Cate extends Model{

    private $cateModel;
    public function __construct(){
        parent::__construct();
        $this -> cateModel = db('cate');
    }

    /*  获取分类的某个字段
     *  @paran Array [wherer]
     *  @paran Array [save]
     *  @return res  result
     * **/
    public function getCateValue($where,$value){
        return $this -> cateModel -> where($where) -> value($value);
    }
    /*  获取行业分类
     *  @param Array [where]
     *  @return Array [data]
     * **/
    public function getCateList( $where = false , $order = false , $type = 1 ){
        $data = $this -> cateModel -> where($where) -> order($order) -> select();
        if(empty($data)){   return [];  }
        switch ($type) {
            case 1://首页行业分类（子集=项目）
                $dataList = $this -> formatCateDataForList($data);
                break;
            case 2://行业分类首页（子集=项目）
                $dataList = $this -> formatCateDataForCateIndex($data);
                break;
            case 3:
                $dataList = $this -> formatCateList($data);
                break;
            case 4://全部行业分类 (子集=子集分类)
                $dataList = $this -> formatCateDataforCateInfo($data);
                break;
            default:
                # code...
                break;
        }
        return $dataList;
    }
    /* 格式化数据(首页。带项目)
     *  @param Array    [data]
     *  @return Array   [list]
     * **/
    public function formatCateDataForList($data){
        $itemModel = new Item();
        $url = config('CATEURL');
        foreach ($data as $key => $value) {
            $list[$key]['id'] = $value['id'];
            $list[$key]['name'] = $value['name'];
            $list[$key]['url'] = $url[$value['id']];
            $list[$key]['data'] = $itemModel -> getItemDataList(['cate_pid'=>$value['id'],'status'=>1,'reco_index'=>1],'id desc',1,7);
        }
        return $list;
    }
    /*  格式化数据（行业分类首页）带项目
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public function formatCateDataForCateIndex($data){
        $itemModel = new Item();
        foreach ($data as $key => $value) {
            $list[$key]['id'] = $value['id'];
            $list[$key]['name'] = $value['name'];
            $list[$key]['data'] = $itemModel -> getItemDataList(['cate_id'=>$value['id'],'status'=>1],'order desc , id desc',1,60);
            $list[$key]['reco'] = $itemModel -> getItemDataList(['cate_id'=>$value['id'],'status'=>1],'read_num desc , id desc',1,10);
        }
        return $list;
    }
    /*  行业分类 name  id
     *  @param Array [data]
     *  @return Array [list]
     * **/
    public function formatCateList($data){
        foreach ($data as $key => $value) {
            $list[$key]['name'] = $value['name'];
            $list[$key]['id'] = $value['id'];
        }
        return $list;
    }
    /*  行业分类  带子集分类
     *  @param Array [data]
     *  @return Array [info]
     * **/
    public function formatCateDataforCateInfo($data){
        $url = config('CATEURL');
        foreach ($data as $key => $value) {
            $info[$key]['name'] = $value['name'];
            $info[$key]['id'] = $value['id'];
            if($value['pid'] == 0){
                $info[$key]['url'] = $url[$value['id']];
            }
            $info[$key]['child'] = $this -> getCateList(['status'=>1,'pid'=>$value['id']],'id asc',3);
        }
        return $info;
    }
    /*  随机获取子分类 && 获取子分类下的5个推荐项目
     *  @param Array [where]
     *  @param int limit
     *  @return Array [dataList]
     * **/
    public function randGetCateAndItem($where,$limit=3){
        $itemModel = new Item();
        $url = config('CATEURL');
        $cateData = $this -> cateModel -> where($where) -> orderRaw('rand()') -> limit($limit) -> select();
        foreach ($cateData as $key => $value) {
            $dataList[$key]['id'] = $value['id'];
            $dataList[$key]['name'] = $value['name'];
            $dataList[$key]['url'] = $url[$value['pid']];
            $dataList[$key]['item'] = $itemModel -> getItemDataList(['status'=>1,'cate_id'=>$value['id'],'reco_index'=>1],'order desc , id desc','1',5);
        }
        return $dataList;

    }


}